using Engine.Graphics;
using Engine.TrafficHelpers;
using Ptx2bmpConverter;
using System;
using System.Collections.Generic;
using System.IO;
using System.Windows.Forms;

namespace street
{
    public partial class PropertiesForCRUI : UserControl
    {
        #region Private Fields

        private PropertiesCR[] crossroadProperties;
        private bool duringControlUpdate = false;
        private Material materialForRail;

        #endregion Private Fields

        #region Public Constructors

        // = new WR2Traffic[10];
        public PropertiesForCRUI()
        {
            InitializeComponent();
            this.duringControlUpdate = true;
            this.crossroadProperties = new PropertiesCR[0];

            this.comboBox_MappingMode.DataSource = Enum.GetValues(typeof(EnumMappingMode));
            this.comboBox_MappingMode.SelectedItem = EnumMappingMode.material144;

            this.checkBox_CRRailRight.Visible = false;
            this.label_RailRight.Visible = false;
            this.label_RailLeft.Text = "Rail";

            this.duringControlUpdate = false;
        }

        #endregion Public Constructors

        #region Public Methods

        public PropertiesCR[] GetCrossroadProperties(int numberOfstreets)
        {
            PropertiesCR[] crossroadProperties = new PropertiesCR[numberOfstreets];
            // If there there are to little

            if (numberOfstreets >= crossroadProperties.Length)
                UpdateNumberOfProperties(numberOfstreets);

            for (int i = 0; i < numberOfstreets; i++)
            {
                crossroadProperties[i] = this.crossroadProperties[i];
            }
            return crossroadProperties;
        }

        public CrossroadTraffic[] GetTrafficDataForCrossroad(PropertiesCR[] roadProperties, int numberOfStreets)
        {
            CrossroadTraffic[] traffic = new CrossroadTraffic[numberOfStreets];

            for (int i = 0; i < numberOfStreets; i++)
            {
                traffic[i] = roadProperties[i].Traffic;
            }
            return traffic;
        }

        public void SetCrossroadProperties(PropertiesCR[] value)
        {
            crossroadProperties = new PropertiesCR[value.Length];
            value.CopyTo(crossroadProperties, 0);
            Index_ValueChanged(null, null);
        }

        public void SetTrafficDataForCrossroad(CrossroadTraffic[] value)
        {
            for (int i = 0; i < value.Length; i++)
            {
                this.crossroadProperties[i].Traffic = value[i];
            }
        }

        #endregion Public Methods

        #region Internal Methods

        internal void GiveTexturesPaths(List<string> texturePaths)
        {
            this.duringControlUpdate = true;
            this.listBox_textureCrossroad.Items.Clear();

            string tempTextureName = Path.GetFileNameWithoutExtension(texturePaths[0]);
            this.materialForRail = new Material(tempTextureName, tempTextureName, tempTextureName, EnumMappingMode.material208);

            foreach (string name in texturePaths)
            {
                string shortName = System.IO.Path.GetFileNameWithoutExtension(name);

                //Fill the listboxes with texturenames
                this.listBox_textureCrossroad.Items.Add(shortName);

                //try to find one of standard asphalt Textures and Create the Material
                string textureName = shortName.ToUpper();
                if (textureName == "ASPH1" || textureName == "ASPH2")
                {
                    int lastTextureID = this.listBox_textureCrossroad.Items.Count - 1;
                    this.listBox_textureCrossroad.SelectedIndex = lastTextureID;
                }

                //try to find one of standard guardrail Textures and Create the Material
                if (textureName == "LEITPLA01")
                {
                    this.materialForRail = new Material(shortName, shortName, shortName, EnumMappingMode.material208);
                }
            }

            this.duringControlUpdate = false;
        }

        #endregion Internal Methods

        #region Private Methods

        private void Index_ValueChanged(object sender, EventArgs e)
        {
            duringControlUpdate = true;
            {
                if (this.numericUpDown_selectedStreet.Value > crossroadProperties.Length)
                    this.numericUpDown_selectedStreet.Value = crossroadProperties.Length;

                int selectedCrossroad = (int)this.numericUpDown_selectedStreet.Value - 1;

                this.crossroadTrafficControl.SetCrossroadTraffic(crossroadProperties[selectedCrossroad].Traffic);

                PropertiesCR cr = crossroadProperties[selectedCrossroad];

                this.comboBox_MappingMode.SelectedItem = cr.MaterialForRoad.MappingMode;
                this.numericUpDown_StreetWidth.Value = (decimal)cr.Streetwidth;
                this.checkBox_CRRailLeft.Checked = Convert.ToBoolean(cr.RailLeft);
                this.checkBox_CRRailRight.Checked = Convert.ToBoolean(cr.RaiRight);

                if (this.radioButton_Tex1.Checked)
                    this.listBox_textureCrossroad.SelectedIndex = streetTool.MaterialManager.GetTextureID(cr.MaterialForRoad.TextureName1);
                if (this.radioButton_Tex2.Checked)
                    this.listBox_textureCrossroad.SelectedIndex = streetTool.MaterialManager.GetTextureID(cr.MaterialForRoad.TextureName2);
                if (this.radioButton_Tex3.Checked)
                    this.listBox_textureCrossroad.SelectedIndex = streetTool.MaterialManager.GetTextureID(cr.MaterialForRoad.TextureName3);
            }
            duringControlUpdate = false;
        }

        private void Propertie_ValueChanged(object sender, EventArgs e)
        {
            if (!duringControlUpdate && crossroadProperties.Length > 0)
            {
                int selectedCrossroad = (int)this.numericUpDown_selectedStreet.Value - 1;

                // Create new data when there are insufficient entries.
                if (crossroadProperties.Length < selectedCrossroad + 1)
                {
                    UpdateNumberOfProperties(selectedCrossroad);
                }

                PropertiesCR cr = crossroadProperties[selectedCrossroad];

                cr.Traffic = this.crossroadTrafficControl.GetCrossroadTraffic();
                cr.Streetwidth = (float)this.numericUpDown_StreetWidth.Value;
                cr.RaiRight = Convert.ToInt32(this.checkBox_CRRailRight.CheckState);
                cr.RailLeft = Convert.ToInt32(this.checkBox_CRRailLeft.CheckState);
                crossroadProperties[selectedCrossroad] = cr;
                if (listBox_textureCrossroad.SelectedIndex > -1)
                {
                    //Update the material
                    string textureName = listBox_textureCrossroad.Items[listBox_textureCrossroad.SelectedIndex].ToString();
                    Material material = cr.MaterialForRoad;
                    material.MappingMode = (EnumMappingMode)this.comboBox_MappingMode.SelectedItem;
                    if (this.radioButton_Tex1.Checked)
                        material.TextureName1 = textureName;
                    if (this.radioButton_Tex2.Checked)
                        material.TextureName2 = textureName;
                    if (this.radioButton_Tex3.Checked)
                        material.TextureName3 = textureName;
                    cr.MaterialForRoad = material;
                }
                crossroadProperties[selectedCrossroad] = cr;

                streetTool.hasPropertiesChanged = true;
            }
            if (listBox_textureCrossroad.SelectedIndex > -1)
            {
                try
                {
                    this.pictureBox_previewCrossroad.Image = Ptx2bmp.FromFile(Application.StartupPath + "\\texture\\" + listBox_textureCrossroad.Items[listBox_textureCrossroad.SelectedIndex].ToString() + ".ptx");
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                }
            }
        }

        private void UpdateNumberOfProperties(int numberOfstreets)
        {
            string textureName = listBox_textureCrossroad.Items[listBox_textureCrossroad.SelectedIndex].ToString();
            int currentNumber = this.crossroadProperties.Length;
            Array.Resize(ref this.crossroadProperties, numberOfstreets);
            for (int i = currentNumber; i < numberOfstreets; i++)
            {
                this.crossroadProperties[i].RailLeft = Convert.ToInt32(this.checkBox_CRRailLeft.Checked);
                this.crossroadProperties[i].RaiRight = Convert.ToInt32(this.checkBox_CRRailRight.Checked);
                this.crossroadProperties[i].Streetwidth = (float)this.numericUpDown_StreetWidth.Value;
                this.crossroadProperties[i].MaterialForRoad = new Material(textureName, textureName, textureName, (EnumMappingMode)this.comboBox_MappingMode.SelectedItem);
                this.crossroadProperties[i].MaterialForRail = this.materialForRail;

                this.crossroadProperties[i].Traffic = this.crossroadTrafficControl.GetCrossroadTraffic();
            }
        }

        #endregion Private Methods
    }
}